package com.platform.server.util;

import java.math.BigInteger;
import java.util.Random;
import java.util.UUID;

/**
 * Created by lenovo on 2016/9/30.
 */
public class UUIDUtil {

    private static final Random random = new Random();

    public UUIDUtil() {
    }

    public static String getUUID() {
        UUID uuid = UUID.randomUUID();
        String idstr = uuid.toString().replace("-", "");
        return idstr.toUpperCase();
    }

//    public static long getLongUUID() {
//        return JedisUtil.getIncr("seq:uuid").longValue();
//    }
//
//    public static long getMaxLongUUID(int num) {
//        return JedisUtil.incrMaxBy("seq:uuid", num).longValue();
//    }

    public static String generateGUID() {
        return (new BigInteger(165, 32, random)).toString(36).toUpperCase();
    }

    public static String generateShortUuid() {
        String[] chars = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");

        for(int i = 0; i < 8; ++i) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 62]);
        }

        return shortBuffer.toString();
    }

    public static void main(String[] args) {
        System.out.println(getOrderSnByTime18());
//        System.out.println(generateGUID());
//        System.out.println(getUUID());
//        System.out.println(getFixLenthInt(6));
//        System.out.println(getFixLenthInt(6));
//        System.out.println(getFixLenthInt_11());
//        System.out.println("genshort---"+generateShortUuid());
    }

    public static long getFixLenthInt_11() {
        int strLength = 12;
        Random rm = new Random();

        // 获得随机数
        double pross = (1 + rm.nextDouble()) * Math.pow(10, strLength);

        // 将获得的获得随机数转化为字符串
        String fixLenthString = String.valueOf(pross);

        // 返回固定的长度的随机数
        return Long.parseLong(fixLenthString.substring(2, strLength + 1));
    }

    public static String getFixLenthInt(int strLength) {
        Random rm = new Random();

        // 获得随机数
        double pross = (1 + rm.nextDouble()) * Math.pow(10, strLength);

        // 将获得的获得随机数转化为字符串
        String fixLenthString = String.valueOf(pross);

        // 返回固定的长度的随机数
        return fixLenthString.substring(1, strLength + 1);
    }

    public static String getOrderSnByTime18(){
        String s=(System.currentTimeMillis()+"").substring(1)+(System.nanoTime()+"").substring(7,10);
        Random rm = new Random();
        double pross = (1 + rm.nextDouble()) * Math.pow(10, 12);
        String fixLenthString = String.valueOf(pross);
        return s+fixLenthString.substring(2, 5);
    }
}
